//
//  YDEmotionBottomView.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/18.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit
private let tagNumber = 2000
//按钮枚举
enum YDEmotionBottomViewType:Int{
//    不要轻易使用tag 为0 可能设置给UIStackView 产生bug
    
    case Default = 0
    case Emoji = 1
    case Lxh = 2
}

// UIStackView 容器视图
class YDEmotionBottomView: UIStackView {

//    01声明一个闭包
    var closure:((type:YDEmotionBottomViewType)->())?
//    记录按钮状态
    var currentButton:UIButton?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    提供方法 供外界调用 关联按钮和collectionview的滚动
    func setupCurrentButton(tag:Int){
//        得到button 传入的tag值是依据section (0,1,2)设置的 而为了解决tag = 0出现的bug 已经将button的tag值 加了2000 所以这里传入的也要加2000
        let button = viewWithTag(tag + tagNumber) as! UIButton
        dealButton(button)
    
    }
    
    
//    监听按钮事件
    func buttonClick(button:UIButton){
  
        dealButton(button)
//        执行闭包
        closure?(type: YDEmotionBottomViewType(rawValue: button.tag - tagNumber)!)
        
        
    
    
    }
//    封装选中按钮的方法
    
    func dealButton(button:UIButton){
        //        如果当前选中的button和这次的相同 不再设置
        if currentButton == button{
            return
        }
        //        取消上个选中的按钮
        currentButton?.selected = false
        //        把当前的选中
        button.selected = true
        //        赋值
        currentButton = button

    
    
    }
    
//    设置视图
    func setupUI(){
    backgroundColor = UIColor.orangeColor()
////        填充方向
//        axis = .Horizontal
////        填充方式
//        distribution = .FillEqually
//        addChildbuttons("left", titel: "默认",type: .Default)
//        addChildbuttons("mid", titel: "Emoji",type:.Emoji)
//        addChildbuttons("right", titel: "浪小花",type: .Lxh)
        
        let defaultButton = addChildbuttons("left", titel: "默认",type: .Default)
        let emojiButton = addChildbuttons("mid", titel: "Emoji",type: .Emoji)
        let lxhButton = addChildbuttons("right", titel: "浪小花",type: .Lxh)
//        添加约束
        defaultButton.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(emojiButton)
            
        }
        emojiButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(defaultButton.snp_trailing)
            make.width.equalTo(lxhButton)
            make.top.bottom.equalTo(self)
        }
        lxhButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(emojiButton.snp_trailing)
            make.top.trailing.bottom.equalTo(self)
        }
    
    }
//    创建按钮
    func addChildbuttons(imageName:String,titel:String,type:YDEmotionBottomViewType)->UIButton{
        let button = UIButton()
        button.tag = type.rawValue + tagNumber
        //        添加事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
      button.setTitle(titel, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
//        默认情况下defaut是选中状态
        if type == .Default{
            button.selected = true
            currentButton = button
        
        }
//        添加按钮
        addSubview(button)
//        addArrangedSubview(button)

        
//        需要做约束 所以要return
        return button
    
    }
}
